package cn.zhaoguang.usedcar.common.util;

import cn.hutool.core.date.DateUtil;
import cn.zhaoguang.usedcar.common.http.ResultVO;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Description
 * @Author zhaoguang
 * @Date 2021/6/23 15:29
 */
public class FileHandleUtil {

    /**
     * 保存文件到本机上
     *
     * @Author zhaoguang
     */
    public static String saveFile(File folder, MultipartFile file) {
        //创建文件夹
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }

        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            //保存文件到本机
            file.transferTo(new File(folder, newName));
            return newName;

        } catch (IOException e) {
            e.printStackTrace();
            return newName;
        }
    }
}
